package 树;

public class Leedcode543二叉树的直径 {
    int maxLen = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        //边界条件
        if (root == null){
            return 0;
        }
        count(root);
        return maxLen;
    }

    private int count(TreeNode root) {

        if (root == null){
            return 0;
        }
        int right = count(root.right);
        int left = count(root.left);
        if (right + left > maxLen)
            maxLen = right + left;
        return Math.max(right, left) + 1;
    }
}
